from logger import logger
from sub_events.base_event import BaseEvent

LOGGER = logger.CustomLogger()

class ReviewEvent(BaseEvent):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.event_name_zh: str = "加分"
        self.event_name_en: str = "Review"

    def pre_execute(self) -> bool:
        return self.hook.commenter in self.OCM.owner_config['userInfos']

    def review(self, group):
        if not group:
            return
        access_token = self.cfg.get("accounts", {}).get("groups")[group]
        response = self.git_client.review(self.hook.pr_number, token=access_token)
        LOGGER.info(
            f"""[{self.hook.pr_number}] {self.hook.commenter} \"pass\" and {group} review successfully, response code {response.status_code}""")
        if self.comment:
            self.comment += "\n"
        self.comment += f"@{self.hook.commenter.split('/')[-1]} 已评审通过, 责任田@{group.split('/')[-1]} 已加分, 满足合入条件后请联系Commiter审核加分"

    def execute(self) :
        user_infos = self.OCM.owner_config['userInfos']
        group_info = user_infos[self.hook.commenter]['group']
        if isinstance(group_info, list):
            for group in group_info:
                self.review(group)
        else:
            self.review(group_info)
